﻿//Copyright (c) 2009, Andreas Grünwald
//All rights reserved.

using System;
using System.Windows;

namespace yaTDL.Core.UI
{

    /// <summary>
    /// Contains methods to show dialogs (currently only MessageBoxes) and makes sure they are shown properly. 
    /// </summary>
    public static class Dialogs
    {
        /// <summary>
        /// As WPF does not allow MessageBoxes if the application does not have a window, 
        /// this invisible window will be shown, before a Messagebox is created and will be closed after the MessageBox has been closed.
        /// </summary>
#if !SILVERLIGHT    
        static Window helperWindow;  
#endif



        /// <summary>
        /// Displays a message box that has a message and that returns a result.
        /// </summary>
        /// <param name="text">A String that specifies the text to display.</param>
        /// <returns>Returns a System.Windows.MessageBoxResult value that specifies which buttons the user clicked.</returns>
        public static MessageBoxResult ShowMessageBox(String text)
        {
#if !SILVERLIGHT
            return ShowMessageBox(text, "", MessageBoxButton.OK, MessageBoxImage.None);
#else
            return ShowMessageBox(text, "", MessageBoxButton.OK);
#endif
        }

        /// <summary>
        /// Displays a message box that has a message, a title bar caption and that returns a result.
        /// </summary>
        /// <param name="text">A String that specifies the text to display.</param>
        /// <param name="title">A String that specifies the title bar caption to display.</param>
        /// <returns>Returns a System.Windows.MessageBoxResult value that specifies which buttons the user clicked.</returns>
        public static MessageBoxResult ShowMessageBox(String text, String title)
        {
#if !SILVERLIGHT    
            return ShowMessageBox(text, title, MessageBoxButton.OK, MessageBoxImage.None);
#else
            return ShowMessageBox(text, title, MessageBoxButton.OK);
#endif
        }

#if !SILVERLIGHT
        /// <summary>
        /// Displays a message box that has a message, title bar caption, buttons and that returns a result.
        /// </summary>
        /// <param name="text">A String that specifies the text to display.</param>
        /// <param name="title">A String that specifies the title bar caption to display.</param>
        /// <param name="buttons">A System.Windows.MessageBoxButton value that specifies which buttons to display.</param>        
        /// <returns>Returns a System.Windows.MessageBoxResult value that specifies which buttons the user clicked.</returns>
        public static MessageBoxResult ShowMessageBox(String text, String title, MessageBoxButton buttons)
        {
            return ShowMessageBox(text, title, buttons, MessageBoxImage.None);
        }
#endif
        

#if !SILVERLIGHT    
        /// <summary>
        /// Displays a message box that has a message, title bar caption, buttons and a icon and that returns a result.
        /// </summary>
        /// <param name="text">A String that specifies the text to display.</param>
        /// <param name="title">A String that specifies the title bar caption to display.</param>
        /// <param name="buttons">A System.Windows.MessageBoxButton value that specifies which buttons to display.</param>
        /// <param name="icon">A System.Windows.MessageBoxImage calue that specifies which icon to display.</param>
        /// <returns>Returns a System.Windows.MessageBoxResult value that specifies which buttons the user clicked.</returns>     
        public static MessageBoxResult ShowMessageBox(String text, String title, MessageBoxButton buttons, MessageBoxImage icon)
#else
        /// <summary>
        /// Displays a message box that has a message, title bar caption, buttons and a icon and that returns a result.
        /// </summary>
        /// <param name="text">A String that specifies the text to display.</param>
        /// <param name="title">A String that specifies the title bar caption to display.</param>
        /// <param name="buttons">A System.Windows.MessageBoxButton value that specifies which buttons to display.</param>       
        /// <returns>Returns a System.Windows.MessageBoxResult value that specifies which buttons the user clicked.</returns>     
        public static MessageBoxResult ShowMessageBox(String text, String title, MessageBoxButton buttons)
#endif

        {
            MessageBoxResult result;

            
#if !SILVERLIGHT    

            if (System.Windows.Threading.Dispatcher.CurrentDispatcher.CheckAccess())
            {
                try
                {
                    helperWindow = new Window()
                    {
                        AllowsTransparency = true,
                        Background = System.Windows.Media.Brushes.Transparent,
                        WindowStyle = System.Windows.WindowStyle.None,
                        Top = 0,
                        Left = 0,
                        Width = 1,
                        Height = 1,
                        ShowInTaskbar = false
                    };

                    helperWindow.Show();
 
                }
                catch (Exception) { }                
                           }

            result = MessageBox.Show(text, title, buttons, icon);

            if(helperWindow != null) helperWindow.Close();
#else
            result = MessageBox.Show(text, title, buttons);
#endif

            return result;
 
        }
    }
}
